//获得绝对高度
function getAbsoluteTop(ob){var s_el=0;el=ob;while(el){s_el=s_el+el.offsetTop ;el=el.offsetParent;}; return s_el;}
function getAbsoluteLeft(htmlObject){
    var XPos = htmlObject.offsetLeft;
    var temp = htmlObject.offsetParent;
    while(temp != null){
        XPos+= temp.offsetLeft;
        temp = temp.offsetParent;
    }
    return XPos;
}
function insertHtml(id, html) {
//    KE.util.focus(id);
//    KE.util.selection(id);
//    KE.util.insertHtml(id, html);
    editor1.html(html);
}

function repHtmlSubmit(){
    var userGread = jQuery("#userGread").val();
    if(bannedIp){
        showThemeAlertPanel("您所在的IP段因有人恶意发帖被封.&nbsp;");
        return false;
    }
    if(if_add_bbs_message){
        showThemeAlertPanel(forum2daylimit);
        return;
    }
    if(bbsReplyTalkCount >= 5 && userGread < 2){
        showThemeAlertPanel(forumreplaylimit);
        return false;
    }

    if(getEditorData().length <= 0)
    {
        alert(forumnoemptylimit);
        return false;
    }

    if(manager){
        checkReply();
        return true;
    }

    /*   	var code =jQuery.trim($("code2").value) ;
     if(code==""){
     refCode2();
     showThemeAlertPanel( forumverifycode);
     return	false;
     }*/

    var title = "";
    var content =  jQuery.trim(getEditorData());
    var url = "/jsp/shequ/filterKeyword.jsp?title="+title+"&content="+encodeURI(content)+"&date="+new Date().getTime();
    var result = trim(jQuery.ajax({url: url, async: false}).responseText);
    if(result == "true"){
        showThemeAlertPanel( forumcharacterslimit);
        return false;
    }

    /*   	var url = '/jsp/shequ/randcheck.jsp?code='+code;
     url=encodeURI(url);
     url=encodeURI(url);
     jQuery("#a5").load(url,
     function (data){
     data = trim(data.replace(/\r\n/gim, ""));
     if(data=="true"){
     checkReply();
     return true;
     }else{
     showThemeAlertPanel( forumverifycode);
     refCode2();
     return false;
     }
     }
     );*/
    checkReply();
    return false;
}


function checkReply()
{
    syncContent();
    document.replyFrm.submit();
}

//1,编辑主题
//2,编辑帖子
//3,回复帖子
//4，引用帖子
//5,发新主题
//6,引用主题
//7,回复主题
//type:0-themes 1-posts       id:themes or posts id  defualt 0
function showEditorPanel(action, id)
{
    var fid = jQuery("#fid2").val();
    var userGread = jQuery("#userGread").val();
    var dailyTheme = jQuery("#dailyTheme").val();

    if(userrole==-1)
    {
        showThemeAlertPanel(forumloginlimit);
        return;
    }

    if(bannedIp){
        showThemeAlertPanel("您所在的IP段因有人恶意发帖被封.&nbsp;");
        return;
    }

    if(perlevel>0 && (action==2 || action==3 || action==4 || action==6 || action==7))
    {
        showThemeAlertPanel("您不具有此操作的权限.&nbsp;");
        return;
    }
    if((perlevel==1 || perlevel==2) && (action ==1 || action == 5) )
    {
        showThemeAlertPanel("您不具有此操作的权限.&nbsp;");
        return;
    }

    if(!manager && allowPost==1 && (action ==1 || action == 5)){
        showThemeAlertPanel(forumofficiallimit);
        return;
    }
    //临时加的逻辑，用户注册24小时之后才能发帖
    if((action == 5 || action == 7 || action == 3) && if_add_bbs_message){
        showThemeAlertPanel("您不具有此操作的权限.&nbsp;");
        return;
    }
    if(action == 5 && bbsTalkCount >= 1 && userGread< 2){
        showThemeAlertPanel("新手一天只能发帖1次!");
        return;
    }
    if((action == 3 || action == 7) && bbsReplyTalkCount >= 5 && userGread< 2){
        showThemeAlertPanel(forumreplaylimit);
        return;
    }

    jQuery("#theme_reply_panel").show();
    if(navigator.userAgent.indexOf("MSIE")>0 && navigator.userAgent.indexOf("MSIE 8.0") < 0) {
        $("theme_reply_panel").style.top = 100+document.documentElement.scrollTop+"px";
    }
    window.onscroll = function(){
        if(navigator.userAgent.indexOf("MSIE")>0 && navigator.userAgent.indexOf("MSIE 8.0") < 0) {
            $("theme_reply_panel").style.top = 100+document.documentElement.scrollTop+"px";
        }
    };
    if (action == 1)
    {
        $("reply_panel_title").innerHTML = forumeditetopic;
        $("theme_reply_panel_submit").value = forumeditetopic;
        $("theme_reply_panel_submit").title = forumeditetopic;
        $("theme_reply_panel_icon").style.display = "block";
        $("img-btn").style.display = "block";
        var posttitle = $("theme_title_" + id).innerHTML;
        $("theme_reply_panel_title").value = posttitle;
        var themecontent = $("theme_content_" + id).innerHTML;
        insertHtml('theme_reply_panel_textarea', formateHtml(themecontent));
        setThemeIcon($('themeiconid_old').value);
        $("theme_reply_form").action = "/shequ/bbsThemeEdit.do?act=edit";
    } else if (action == 2)
    {
        $("reply_panel_title").innerHTML = forumeditpost;
        $("theme_reply_panel_submit").value = forumeditpost;
        $("theme_reply_panel_submit").title = forumeditpost;
        $("theme_reply_panel_icon").style.display = "none";
        $("img-btn").style.display = "none";
        var posttitle = $("post_title_" + id).innerHTML;
        $("theme_reply_panel_title").value = posttitle;
        var postcontent = $("post_content_" + id).innerHTML;
        insertHtml('theme_reply_panel_textarea', formateHtml(postcontent));
        $("theme_reply_form").action = "/shequ/bbsPostEdit.do?act=edit&pid=" + id;
    }
    else if (action == 3)
    {
        $("reply_panel_title").innerHTML = forumreplaypost;
        $("theme_reply_panel_submit").value = forumreply;
        $("rid").value = id;
        $("theme_reply_panel_submit").title = forumreplaypost;
        $("theme_reply_panel").style.display = "block";
        $("theme_reply_panel_icon").style.display = "none";
        $("img-btn").style.display = "none";
        var posttitle = "RE:" + $("post_title_" + id).innerHTML;
        $("theme_reply_panel_title").value = posttitle;
        $("theme_reply_form").action = "/shequ/bbsPostEdit.do";
    }
    else if (action == 4)
    {
        $("reply_panel_title").innerHTML = forumreplaypost;
        $("theme_reply_panel_submit").value = forumreply;
        $("rid").value = id;
        $("theme_reply_panel_submit").title = forumreplaypost;
        $("theme_reply_panel_icon").style.display = "none";
        $("img-btn").style.display = "none";
        var posttitle = "RE:" + $("post_title_" + id).innerHTML;
        $("theme_reply_panel_title").value = posttitle;
        var postcontent = getQuoteHtml($("post_content_" + id).innerHTML);
        var quotehtml = '<div class="quote"><blockquote>' + postcontent + '</blockquote></div><br>';
        insertHtml('theme_reply_panel_textarea', formateHtml(quotehtml));
        $("theme_reply_form").action = "/shequ/bbsPostEdit.do";
    }
    else if (action == 5)
    {
        $("reply_panel_title").innerHTML = forumnewtopic;
        $("theme_reply_panel_submit").value = forumnewtopic;
        $("theme_reply_panel_submit").title = forumnewtopic;
        $("theme_reply_panel").style.display = "block";
        $("theme_reply_panel_icon").style.display = "block";
        $("img-btn").style.display = "block";
        $("theme_reply_panel_title").value = "";
        setThemeIcon(1);
        $("theme_reply_form").action = "/shequ/bbsThemeEdit.do";
    } else if (action == 6)
    {
        $("reply_panel_title").innerHTML = forumreplaytopic;
        $("theme_reply_panel_submit").value = forumreply;
        $("theme_reply_panel_submit").title = forumreplaytopic;
        $("theme_reply_panel_icon").style.display = "none";
        $("img-btn").style.display = "none";
        var posttitle = "RE:" + $("theme_title_" + id).innerHTML;
        $("theme_reply_panel_title").value = posttitle;
        var themecontent = getQuoteHtml($("theme_content_" + id).innerHTML);
        var quotehtml = '<div class="quote"><blockquote>' + themecontent + '</blockquote></div><br>';
        insertHtml('theme_reply_panel_textarea', formateHtml(quotehtml));
        $("theme_reply_form").action = "/shequ/bbsPostEdit.do";
    }
    else if (action == 7)
    {
        $("reply_panel_title").innerHTML = forumreplaytopic;
        $("theme_reply_panel_submit").value = forumreply;
        $("theme_reply_panel_submit").title = forumreplaytopic;
        $("theme_reply_panel").style.display = "block";
        $("theme_reply_panel_icon").style.display = "none";
        $("img-btn").style.display = "none";
        var themetitle = "RE:" + $("theme_title_" + id).innerHTML;
        $("theme_reply_panel_title").value = themetitle;
        $("theme_reply_form").action = "/shequ/bbsPostEdit.do";
    }
    /**刷验证码**/
    if(!manager){
        //refCode();
    }
}

function formateHtml(source){
    raRegExp = new RegExp("\n","g");
    return source.replace(raRegExp,'');
}

function hiddenReplyPanel()
{
    $("theme_reply_panel").style.display = "none";
    window.onscroll = null;
}

function setThemeIcon(iconid)
{
    $('themeiconid').value = iconid;
    var o = document.getElementById("img-btn");
    o.getElementsByTagName("img")[0].src = document.getElementById("icon_" + iconid).src;
    o.getElementsByTagName("img")[0].alt = document.getElementById("icon_" + iconid).alt;
}

function checkButtonReplySubmit(){
    //临时加的逻辑，用户注册24小时之后才能发帖
    if(if_add_bbs_message){
        showThemeAlertPanel("您不具有此操作的权限.&nbsp;");
        return;
    }
    var title =  jQuery.trim($("theme_reply_panel_title").value);
    var content =  jQuery.trim(editor1.html());
    if (title == "" ||  content =="") {
        $("theme_reply_panel_error").innerHTML = forumcontentvalidate;
        return false;
    }

    if(manager){
        replaySubmit();
        return true;
    }
    var reg1 = /&(amp;)?#\w{5};/i;
    var result1 =  reg1.exec(title);
    var result2 =  reg1.exec(content);
    if(result1 != null || result2 != null){
        $("theme_reply_panel_error").innerHTML = forumnocodelimit;
        return false;
    }

    /*   	var code =jQuery.trim($("code").value) ;
     if(code==""){
     refCode();
     $("theme_reply_panel_error").innerHTML = forumverifycode;
     return	false;
     }*/

    var url = "/jsp/shequ/filterKeyword.jsp?title="+title+"&content="+encodeURI(content)+"&date="+new Date().getTime();
    var result = trim(jQuery.ajax({url: url, async: false}).responseText);
    if(result == "true"){
        $("theme_reply_panel_error").innerHTML = forumcharacterslimit;
        return false;
    }

    /*   	var url = '/jsp/shequ/randcheck.jsp?code='+code;
     url=encodeURI(url);
     url=encodeURI(url);
     jQuery("#a5").load(url,
     function (data){
     data = trim(data.replace(/\r\n/gim, ""));
     if(data=="true"){
     replaySubmit();
     }else{
     $("theme_reply_panel_error").innerHTML = forumverifycode;
     refCode();
     return false;
     }
     }
     );*/
    replaySubmit();
    return true;
}


function replaySubmit(){
    syncContentReply();
    document.replyPanelFrm.submit();
}

function checkReplySubmit()
{
    if (jQuery.trim($("theme_reply_panel_title").value) == "" ||  jQuery.trim(KE.util.getData("theme_reply_panel_textarea")) =="") {
        $("theme_reply_panel_error").innerHTML = forumcontentvalidate;
        return false;
    }
    return true;

}

function showReportPanel(report_a ,type,id){
    if(userrole==-1)
    {
        showThemeAlertPanel(forumloginlimit);
        return;
    }
    $("report_result").innerHTML = "";
    $("report_otype").value = type;
    $("report_oid").value = id;
    if(type==0){
        $("report_otitle").value = $("theme_title_" + id).innerHTML;;
    }
    else if(type==1)
    {
        $("report_otitle").value = $("post_title_" + id).innerHTML;;
    }
    $("themes_report_panel").style.display = "";
    $("themes_report_panel").style.top = (getAbsoluteTop(document.getElementById(report_a))+20)+"px";
    $("themes_report_panel").style.left = (getAbsoluteLeft(document.getElementById(report_a))-300) + "px";
}
function hiddenReportPanel()
{
    jQuery("#themes_report_panel").hide();
}

function report()
{
    var report_otid = $("report_otid").value;
    var report_otype = $("report_otype").value;
    var report_oid = $("report_oid").value;
    var report_otitle = $("report_otitle").value;
    var report_fid = $("report_fid").value;
    var report_type=0;
    jQuery("input[name=report_type]").each(function (index){
        if (this.checked){
            report_type = this.value;
        }
    });
    var my_report = jQuery("#my_report").val();
    if(my_report=="")
    {
        $("report_result").innerHTML = forumreportemptylimit;
        return;
    }
    var param={report_otid:report_otid,report_otype:report_otype,report_oid:report_oid,report_otitle:report_otitle,report_fid:report_fid,report_type:report_type,my_report:my_report};
    jQuery.post("/jsp/shequ/themesReport.jsp", param,
        function(data){
            if(data){
                $("report_result").innerHTML = forumreportsuccess;
                hiddenReportPanel();
            }
        });
}
function hiddenManagePanel()
{
    $("posts_manage_panel").style.display = "none";
}
function showManagePanel(manager_checkbox)
{
    if(userrole==-1)
    {
        showThemeAlertPanel(forumloginlimit);
        return;
    }
    var managePostNum = 0;
    jQuery("input[name=post_checkbox]").each(function(i){
        if(this.checked)
            managePostNum++;
    });
    if(managePostNum==0)
    {
        $("posts_manage_panel").style.display = "none";
        return;
    }
    $("managePostsNum").innerHTML = managePostNum;
    $("posts_manage_panel").style.display = "block";
    $("posts_manage_panel").style.top = (getAbsoluteTop(document.getElementById(manager_checkbox))+20)+"px";
    $("posts_manage_panel").style.left = (getAbsoluteLeft(document.getElementById(manager_checkbox))-300) + "px";
}

function deletePosts()
{
    if(userrole==-1)
    {
        showThemeAlertPanel(forumloginlimit);
        return;
    }
    var delPostsId = "";
    jQuery("input[name=post_checkbox]").each(function(i){
        if(this.checked)
        {
            if(delPostsId.length>0)
                delPostsId += (","+jQuery(this).val());
            else
                delPostsId += jQuery(this).val();
        }
    });
    var param={delPostsId:delPostsId,type:1};
    jQuery.post("/jsp/shequ/themesManage.jsp", param,
        function(data){
            if(data && data>0){
                var delPostsArr = delPostsId.split(",");
                for(var i=0;i<delPostsArr.length;i++)
                {
                    $("theme_post_"+delPostsArr[i]).style.display = "none";
                    $("theme_post_"+delPostsArr[i]).innerHTML = "";
                    $("theme_post_"+delPostsArr[i]).parentNode.removeChild($("theme_post_"+delPostsArr[i]));
                }
            }
            hiddenManagePanel();
        });
}

function showThemeManagePanel()
{
    if(userrole==-1)
    {
        showThemeAlertPanel(forumloginlimit);
        return;
    }
    $("theme_manage_panel").style.display = "block";
    $("theme_manage_panel").style.top = (getAbsoluteTop(document.getElementById("manage_theme_a"))+20)+"px";
    $("theme_manage_panel").style.left = (getAbsoluteLeft(document.getElementById("manage_theme_a"))-200) + "px";
}
function hiddenThemeManagePanel()
{
    $("theme_manage_panel").style.display = "none";
}

function deleteTheme(tid,fid)
{
    if(userrole==-1)
    {
        showThemeAlertPanel(forumloginlimit);
        return;
    }
    var param={tid:tid,type:0,fid:fid};
    jQuery.post("/jsp/shequ/themesManage.jsp", param,
        function(data){
            if(data && data>00){
                window.location = "/tlist-"+fid+".html";
                hiddenThemeManagePanel();
            }
        });
}

function hiddenThemeAlertPanel()
{
    $("theme_alert_panel").style.display = "none";
    window.onscroll = null;
}

function showThemeAlertPanel(info)
{
    setPositionMiddle(jQuery("#theme_alert_panel"));
    jQuery("#theme_alert_panel").show();
    jQuery("#theme_alert_info").html(info);
}

function setPositionMiddle(object)
{
    var isIE6=false;
    var Sys = {};
    var ua = navigator.userAgent.toLowerCase();
    var s;
    (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1] : 0;
    if(Sys.ie && Sys.ie=="6.0"){
        isIE6=true;
    }
    var windowWidth,windowHeight;//窗口的高和宽
    if (self.innerHeight) {
        windowWidth=self.innerWidth;
        windowHeight=self.innerHeight;
    }else if (document.documentElement&&document.documentElement.clientHeight) {
        windowWidth=document.documentElement.clientWidth;
        windowHeight=document.documentElement.clientHeight;
    } else if (document.body) {
        windowWidth=document.body.clientWidth;
        windowHeight=document.body.clientHeight;
    }
    var l=0;//居左
    var t=0;//居上
    l=windowWidth/2-jQuery(object).width()/2;
    t=windowHeight/2-jQuery(object).height()/2;
    var loc={left:l+"px",top:t+"px"};
    jQuery(object).css(loc).css({position:"fixed",z_index:"999"});
    if (isIE6)
    {
        jQuery(object).css("top",document.documentElement.scrollTop+t);
        jQuery(object).css("position","absolute");
    }
}

function getQuoteHtml(html)
{
    var txthtml = getHtmlTxt(html);
    return subHtml(txthtml,0,250);
}

function getHtmlTxt(html)
{
    var data = html;
    data = data.replace(/<blockquote>(.|\n)*?<\/blockquote>/,'');
    data = data.replace(/<BLOCKQUOTE>(.|\n)*?<\/BLOCKQUOTE>/,'');
    data = data.replace(/<(?!img|embed).*?>/ig, '');
    data = data.replace(/&nbsp;/ig, ' ');
    data = jQuery.trim(data);
    return data;
}

function subHtml(htmlstr,start,length){
    var sourcelength = htmlstr.length;
    var sstr = htmlstr.substr(start,length);
    if(/&\w+$/.test(sstr))
    {
        var estr = htmlstr.substr(start + length,htmlstr.length - start - length);
        sstr += /^.*?;/.exec(estr);
    }
    else if(/<[^>]*$/.test(sstr))
    {
        var estr = htmlstr.substr(start + length,htmlstr.length - start - length);
        sstr += /^.*?>/.exec(estr);
    }
    if(sstr.length<sourcelength)
        sstr += " ...";
    return sstr;
}



function refCode(){
    document.getElementById('regimg').src="/jsp_cn/shequ/include/rand.jsp?"+getTime();
}


function refCode2(){
    document.getElementById('regimg2').src="/jsp_cn/shequ/include/rand.jsp?"+getTime();
}

function getTime(){
    var d, s = "";
    var c = "";
    d = new Date();
    s += d.getYear()+c;
    s += (d.getMonth() + 1) + c;
    s += d.getDate() + c;
    s += d.getHours() + c;
    s += d.getMinutes() + c;
    s += d.getSeconds() + c;
    s += d.getMilliseconds();
    return s;
}
